﻿using System.Data.Entity;
using Sumen.Model.Model;

namespace Sumen.Business.Factory
{
    public class EntityFactory : AbstractFactory
    {
        static EntityFactory entityFactory = new EntityFactory();

        public static EntityFactory getInstance()
        {
            return entityFactory;
        }

        private EntityFactory() { }


        private sumenEntities sumenEntityTrans;// = new sumenEntities();
        private sumenEntitiesSystem sumenEntitySystemTrans;// = new sumenEntitiesSystem();


        public DbContextTransaction SumenTransaction { get; set; }
        public DbContextTransaction SystemTransaction { get; set; }


        public override sumenEntities CreateEntities()
        {
            sumenEntities obj = new sumenEntities();
            obj.Configuration.LazyLoadingEnabled = false;
            obj.Configuration.ProxyCreationEnabled = false;
            return obj;
        }

        public override sumenEntities CreateLazyEntities()
        {
            sumenEntities obj = new sumenEntities();
            return obj;
        }

        public override sumenEntities CreateUpdateTransactionEntities()
        {
            if (sumenEntityTrans == null)
            {
                sumenEntityTrans = new sumenEntities();
            }
            return sumenEntityTrans;
        }

        public DbContextTransaction BeginTransactionEntities()
        {
            if (sumenEntityTrans == null)
            {
                sumenEntityTrans = new sumenEntities();
            }
            if (SumenTransaction == null)
            {
                SumenTransaction = sumenEntityTrans.Database.BeginTransaction();
            }
            return SumenTransaction;
        }

        public override void commit()
        {
            try
            {
                SumenTransaction.Commit();
            }
            catch
            {
                rollBack();
            }
            finally
            {
                sumenEntityTrans = null;
                SumenTransaction = null;
                CreateUpdateTransactionEntities();
            }
        }

        public override void rollBack()
        {
            SumenTransaction.Rollback();
            sumenEntityTrans = null;
            SumenTransaction = null;
            CreateUpdateTransactionEntities();
        }






        public override sumenEntitiesSystem CreateSystemEntities()
        {
            sumenEntitiesSystem obj = new sumenEntitiesSystem();
            obj.Configuration.LazyLoadingEnabled = false;
            obj.Configuration.ProxyCreationEnabled = false;
            return obj;
        }

        public override sumenEntitiesSystem CreateSystemLazyEntities()
        {
            sumenEntitiesSystem obj = new sumenEntitiesSystem();
            return obj;
        }

        public override sumenEntitiesSystem CreateSystemUpdateTransactionEntities()
        {
            if (sumenEntitySystemTrans == null)
            {
                sumenEntitySystemTrans = new sumenEntitiesSystem();
            }
            return sumenEntitySystemTrans;
        }

        public DbContextTransaction BeginTransactionSystemEntities()
        {
            if (sumenEntitySystemTrans == null)
            {
                sumenEntitySystemTrans = new sumenEntitiesSystem();
            }
            if (SystemTransaction == null)
            {
                SystemTransaction = sumenEntitySystemTrans.Database.BeginTransaction();
            }
            return SystemTransaction;
        }

        public override void commitSystem()
        {
            try
            {
                SystemTransaction.Commit();
            }
            catch
            {
                rollBackSystem();
            }
            finally
            {
                sumenEntitySystemTrans = null;
                SystemTransaction = null;
                CreateSystemUpdateTransactionEntities();
            }
        }

        public override void rollBackSystem()
        {
            SystemTransaction.Rollback();
            sumenEntitySystemTrans = null;
            SystemTransaction = null;
            CreateSystemUpdateTransactionEntities();
        }
    }
}
